<template>
	<view class="container">
		<view class="vacancy-list">
			<u-card v-for="(item, index) in publishedList" :key="index" :title="item.vacancyName" :sub-title="'发布时间:' + new Date(item.createTime).toLocaleString()">
				<view slot="body">
					<view class="vacancy-info">
						<text class="salary">薪资：{{item.salary}}</text>
						<text class="address">地点：{{item.address}}</text>
					</view>
					<view class="vacancy-stats">
						<text>申请人数：{{item.applyCount || 0}}</text>
					</view>
				</view>
				<view slot="foot" class="card-foot">
					<u-button type="primary" size="mini" @click="editVacancy(item)">编辑</u-button>
					<u-button type="error" size="mini" @click="deleteVacancy(item)">删除</u-button>
				</view>
			</u-card>
		</view>
		<u-loadmore :status="loadStatus" />
		
		<!-- 悬浮按钮 -->
		<view class="fab-button">
			<u-button type="primary" icon="plus" @click="addVacancy">发布岗位</u-button>
		</view>
	</view>
</template>

<script>
import { pageSearch, deleteVacancy } from '../../utils/api/modules/vacancy';

export default {
	data() {
		return {
			publishedList: [],
			pageNum: 1,
			pageSize: 10,
			loadStatus: 'loading'
		}
	},
	onLoad() {
		this.loadData();
	},
	onPullDownRefresh() {
		this.pageNum = 1;
		this.loadData();
	},
	onReachBottom() {
		if (this.loadStatus === 'loadmore') {
			this.pageNum++;
			this.loadData();
		}
	},
	methods: {
		async loadData() {
			const user = uni.getStorageSync('user');
			if (!user || !user.id) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				return;
			}
			
			try {
				const res = await pageSearch({
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					posterId: user.id
				});
				
				if (this.pageNum === 1) {
					this.publishedList = res.data.records;
				} else {
					this.publishedList = [...this.publishedList, ...res.data.records];
				}
				
				this.loadStatus = this.pageNum * this.pageSize >= res.data.total ? 'nomore' : 'loadmore';
				uni.stopPullDownRefresh();
			} catch (error) {
				console.error('加载数据失败:', error);
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				});
			}
		},
		addVacancy() {
			uni.navigateTo({
				url: '/pages/vacancy/vacancy-add'
			});
		},
		editVacancy(item) {
			uni.navigateTo({
				url: `/pages/vacancy/vacancy-add?id=${item.id}`
			});
		},
		deleteVacancy(item) {
			uni.showModal({
				title: '提示',
				content: '确定要删除该岗位吗？',
				success: async (res) => {
					if (res.confirm) {
						try {
							await deleteVacancy(item.id);
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							});
							this.loadData();
						} catch (error) {
							uni.showToast({
								title: '删除失败',
								icon: 'none'
							});
						}
						setTimeout(()=>{
							window.location.reload()
						},800)
					}
				}
			});
		}
	}
}
</script>

<style>
.container {
	padding: 20rpx;
}

.vacancy-list {
	margin-top: 20rpx;
}

.vacancy-info {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 10rpx;
}

.salary {
	color: #ff5500;
	font-weight: bold;
}

.address {
	color: #666;
	font-size: 28rpx;
}

.vacancy-stats {
	color: #666;
	font-size: 28rpx;
}

.card-foot {
	display: flex;
	justify-content: flex-end;
	gap: 20rpx;
}

.fab-button {
	position: fixed;
	right: 30rpx;
	bottom: 30rpx;
	z-index: 999;
}
</style> 